
$(".userEdit").click(function () {
    var userId = $(this).parent().parent().children(0).html().toString();
    var dialogId = $("#userId").val().toString();
    if(dialogId!=userId) {
        $.get("/user/getbyid", {userID: userId}, function (data) {
            var jsonStr = JSON.stringify(data);
            var jsonObj = JSON.parse(jsonStr);
            var userName = jsonObj.userName;
            var userAccount = jsonObj.userAccount;
            var userId = jsonObj.userID;
            var mail = jsonObj.mail;
            var passWd = jsonObj.passWd;
            var tel = jsonObj.tel;
            var groupId = jsonObj.groupID.toString();
            //填充
            $("#userAccount").val(userAccount);
            $("#userName").val(userName);
            $("#mail").val(mail);
            $("#tel").val(tel);
            $("#passwd").val(passWd);
            $("#userId").val(userId);
            $("#editGroup").val(groupId);
        }, "json");
    }

});


$("#userUpdate").click(function () {
    $("#updateForm").attr("action", "/user/updateuser").submit();
});

$("#addSubmit").click(function () {
    $("#addForm").submit();
});

//删除用户
$(".delete").click(function () {
    var delUri = $(this).attr("del_uri");
    layer.confirm('删除选中?', {icon: 2, title: '删除'}, function () {
        //删除当前用户
        $("#deleteUserForm").attr("action", delUri).submit();
    });
});


$("#generateUser").click(function () {
    var selectUserGroup = $("#user_group option:selected").val();
    var groupName = $("#user_group option:selected").html();
    var userNum = $("#userNumber").val();
    if(selectUserGroup==0){
        selectUserGroup = 2;
        groupName="游客";
    }
    if (userNum == "") {
        layer.alert('数字不能为空', {icon: 2});
    } else {
        $.post("/user/generateuser",{userNum:userNum,groupId:selectUserGroup},function (result,status) {
        });
        layer.msg('生成成功',{icon:6});
        window.location.reload();
    }

});



// 页面加载时触发，动态加载选择框
window.onload = function () {
    var addGroupSelect = $("#user_group");
    var editGroupSelect = $("#editGroup");
    var addOneGroupSelect = $("#group");
    $.get("/group/all", {}, function (data) {
        var jsonStr = JSON.stringify(data).toString();
        //alert(jsonStr);
        var jsonObj = JSON.parse(jsonStr);
        //jsonStr = [{"groupID":4,"domainName":"荆州市","isAnalysis":1,"isNotice":1,"isUpload":1,"isAlarm":1,"isThreshold":1,"isDelete":1,"domainID":14},{"groupID":1,"domainName":"aaa","isAnalysis":1,"isNotice":1,"isUpload":1,"isAlarm":1,"isThreshold":1,"isDelete":1,"domainID":1}]
        addGroupSelect.prepend("<option value='0'>---请选择---</option>");//添加第一个option值
        addOneGroupSelect.prepend("<option value='0'>---请选择---</option>");//添加第一个option值
        for (var i in jsonObj) {
            if(jsonObj[i].groupID>0) {
                addGroupSelect.append("<option value='" + jsonObj[i].groupID + "'>" + jsonObj[i].groupName + "</option>");
                addOneGroupSelect.append("<option value='" + jsonObj[i].groupID + "'>" + jsonObj[i].groupName + "</option>");
                editGroupSelect.append("<option value='" + jsonObj[i].groupID + "'>" + jsonObj[i].groupName + "</option>");

            }
        }
    });

};






